﻿<MCard>
    <MCardTitle>
        <span class="text-h5">Nginx 服务器配置</span>
    </MCardTitle>

    <MCardText>
        <MForm @ref="_form" Model="Config">
            <MContainer>
                <MRow>
                    <MCol Cols="12" Sm="6">
                        <MTextField @bind-Value="Config.ListenPort"
                                    Label="监听端口"
                                    Type="number"
                                    Rules="@PortRules"
                                    Outlined />
                    </MCol>

                    <MCol Cols="12" Sm="6">
                        <MTextField @bind-Value="Config.ServerName"
                                    Label="服务器名称"
                                    TValue="string"
                                    Rules="@ServerNameRules"
                                    Outlined />
                    </MCol>
                </MRow>

                <!-- 前端项目根目录配置 -->
                <MRow>
                    <MCol Cols="12">
                        <MTextField @bind-Value="Config.RootPath"
                                    Label="前端项目根目录"
                                    Hint="例如: /var/www/html"
                                    PersistentHint
                                    Outlined />
                    </MCol>
                </MRow>

                <!-- 默认索引文件配置 -->
                <MRow>
                    <MCol Cols="12">
                        <MSelect @bind-Value="Config.IndexFiles"
                                 Items="@IndexFileOptions"
                                 ItemText="item => item"
                                 ItemValue="item => item"
                                 Label="默认索引文件"
                                 Multiple
                                 Chips
                                 Outlined
                                 DeletableChips>
                        </MSelect>
                    </MCol>
                </MRow>

                <!-- HTTPS 配置 -->
                <MRow>
                    <MCol Cols="12">
                        <MCheckbox @bind-Value="Config.EnableHttps"
                                   Label="启用 HTTPS"
                                   Class="mt-3" />
                    </MCol>
                </MRow>

                @if (Config.EnableHttps)
                {
                    <MRow>
                        <MCol Cols="12" Sm="6">
                            <MTextField @bind-Value="Config.SslCertificatePath"
                                        Label="SSL 证书路径"
                                        Rules="@SslCertRules"
                                        Outlined />
                        </MCol>

                        <MCol Cols="12" Sm="6">
                            <MTextField @bind-Value="Config.SslCertificateKeyPath"
                                        Label="SSL 私钥路径"
                                        Rules="@SslKeyRules"
                                        Outlined />
                        </MCol>
                    </MRow>
                }

                <!-- 额外指令 -->
                <MRow>
                    <MCol Cols="12">
                        <span class="text-subtitle-2">额外指令</span>
                        @for (int i = 0; i < Config.AdditionalDirectives.Count; i++)
                        {
                            var index = i; // 创建局部变量
                            <MRow Class="d-flex align-center mt-2">
                                <MCol Cols="11">
                                    <MTextField @bind-Value="Config.AdditionalDirectives[index]"
                                                Outlined
                                                Class="flex-grow-1" />
                                </MCol>
                                <MCol Cols="1">
                                    <MIconButton Icon="mdi-delete"
                                                 Color="error"
                                                 OnClick="() => RemoveAdditionalDirective(index)" />
                                </MCol>
                            </MRow>
                        }
                        <MButton Outlined
                                 Color="primary"
                                 Class="mt-2"
                                 OnClick="AddAdditionalDirective"
                                 PrependIcon="mdi-plus">
                            添加指令
                        </MButton>
                    </MCol>
                </MRow>

                <!-- Location 配置 -->
                <MRow>
                    <MCol Cols="12">
                        <span class="text-subtitle-2 mt-4">路径配置</span>

                        <MExpansionPanels Multiple>
                            @for (int i = 0; i < Config.LocationList.Count; i++)
                            {
                                var index = i;
                                var currentLocation = Config.LocationList[index];
                                <MExpansionPanel>
                                    <MExpansionPanelHeader Context="panelHeader">
                                        <span>@currentLocation.Path</span>
                                    </MExpansionPanelHeader>
                                    <MExpansionPanelContent>
                                        <NginxLocationEditor Location="currentLocation"
                                                             OnRemove="() => RemoveLocation(currentLocation)" />
                                    </MExpansionPanelContent>
                                </MExpansionPanel>
                            }
                        </MExpansionPanels>

                        <MButton Outlined
                                 Color="primary"
                                 Class="mt-2"
                                 OnClick="AddNewLocation"
                                 PrependIcon="mdi-plus">
                            添加路径配置
                        </MButton>
                    </MCol>
                </MRow>
            </MContainer>
        </MForm>
    </MCardText>

    <MCardActions>
        <MButton Color="primary"
                 OnClick="SaveConfigAsync"
                 PrependIcon="mdi-content-save">
            保存配置
        </MButton>
        <MButton Text
                 OnClick="OnCancel">
            取消
        </MButton>
    </MCardActions>
</MCard>

@code {
    private MForm _form;

    // 可选的索引文件列表
    private List<string> IndexFileOptions = new List<string> { "index.html", "index.htm" };

    [Parameter]
    public NginxServerConfig Config { get; set; } = new NginxServerConfig();

    [Parameter]
    public EventCallback<NginxServerConfig> OnSave { get; set; }

    [Parameter]
    public EventCallback OnCancel { get; set; }
    [Inject]
    public IPopupService PopupService { get; set; }

    #region 验证规则
    private List<Func<int, StringBoolean>> PortRules => new()
    {
        v => v > 0 && v <= 65535 ? true : "端口必须在1-65535之间"
    };

    private List<Func<string, StringBoolean>> ServerNameRules => new()
    {
        v => !string.IsNullOrWhiteSpace(v) ? true : "服务器名称不能为空"
    };

    private List<Func<string, StringBoolean>> SslCertRules => new()
    {
        v => !string.IsNullOrWhiteSpace(v) ? true : "SSL证书路径不能为空"
    };

    private List<Func<string, StringBoolean>> SslKeyRules => new()
    {
        v => !string.IsNullOrWhiteSpace(v) ? true : "SSL私钥路径不能为空"
    };
    #endregion

    private void AddAdditionalDirective()
    {
        Config.AdditionalDirectives.Add("");
    }

    private void RemoveAdditionalDirective(string directive)
    {
        Config.AdditionalDirectives.Remove(directive);
    }

    private void AddNewLocation()
    {
        Config.LocationList.Add(new NginxServerConfigLocation
            {
                Path = "/new-path/",
                ProxyPass = "http://localhost:8080/"
            });
    }

    private void RemoveLocation(NginxServerConfigLocation location)
    {
        Config.LocationList.Remove(location);
    }

    private async Task SaveConfigAsync()
    {
        if (_form.Validate())
        {
            await OnSave.InvokeAsync(Config);
            await PopupService.EnqueueSnackbarAsync("配置保存成功!", AlertTypes.Success);
        }
    }
}